<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%--
  Created by IntelliJ IDEA.
  User: qupen
  Date: 2016/9/18
  Time: 19:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:include page="../include/head.jsp">
    <jsp:param name="title" value="平台管理员列表"/>
</jsp:include>

<jsp:include page="../include/admin_header.jsp"/>

<jsp:include page="../include/admin_sidebar.jsp"/>
<div class="content-wrapper content-wrapper-bg">
    <section class="content-header">
        <h4 class="page-title" style="display: inline-block"><spring:message code="label.user.list"/></h4>
        <a class="sie-head-add" href="/admin/user/createUser#adminUsers" style="margin-left:44px;">
            <i class="fa fa-plus" style="margin-right: 3px"></i><spring:message code="label.user.add"/>
        </a>

        <div class="sie-search-parent">
            <input type="text" placeholder='<spring:message code="label.admin.search"/>' class="form-control sie-search-input" id="inputSuccess2">
            <img src="/images/search.png" class="sie-search-img"/>
        </div>
    </section>
    <div id = "sdsdsd"></div>
    <section class="content">
        <div class="box box-primary table-header-line">
            <div class="box-body" style="padding: 0">
                <table class="table" id="user_list">
                    <thead>
                    <tr>
                        <th><spring:message code="label.admin.user.list.name"/></th>
                        <th><spring:message code="label.admin.user.list.phone"/></th>
                        <th><spring:message code="label.admin.user.list.department"/></th>
                        <th><spring:message code="label.admin.service.state"/></th>
                        <th><spring:message code="label.admin.user.list.operation"/></th>
                    </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
            </div>
        </div>
        <%--</div>--%>
    </section>
</div>
<jsp:include page="../include/foot.jsp"/>
<script>
    $(document).ready(function () {
        var options ={
            "dom": '<"datatable-header"<"dataTables_filter">l><"datatable-scroll"t><"datatable-footer dataTable-footer-padding"<"pull-left"i><"pull-right"p>>',
            paging: true,
            lengthChange: false,
            ordering: true,
            autoWidth: false,
            info: true,
            serverSide: true,
            fixedHeader: true,
            searching: true,
            aLengthMenu: [15],
            ajax: {
                url: '/admin/api/v1/user/list',
                dataSrc: 'data'
            },
            columns: [
                {
                    data: 'user_name'
                },
                {
                    data: 'telephone'
                },
                {
                    data: 'department'
                },
                {
                    data: 'enabled',
                    "render": function (data, type, full, meta) {
                        if (data == 0) {
                            return '<spring:message code="label.tenant.service.not.available"/>';
                        } else if (data == 1) {
                            return '<spring:message code="label.tenant.service.available"/>'
                        } else {
                            return '<spring:message code="label.tenant.data.abnormal"/>';
                        }
                    }
                },
                {
                    data: 'user_id', width: "350px",
                    "render": function (data, type, full, meta) {

                        var divString;
                        if(full.user_name=="admin"){
                            divString= '<div class="sie-btn-group">' +
                            '<a href="/admin/user/setInfo?id=' + data + '#adminUsers"><i class="sie-btn-edit"></i><span><spring:message code="label.user.edit"/></span></a>' +
                            '<a href="javascript:;" onclick="resetPassword(\'' + data + '\')"><i class="sie-btn-reset"></i><span><spring:message code="label.user.reset.password"/></span></a>';
                        }else{
                            divString= '<div class="sie-btn-group">' +
                                    '<a href="/admin/user/setInfo?id=' + data + '#adminUsers"><i class="sie-btn-edit"></i><span><spring:message code="label.user.edit"/></span></a>' +
                                    '<a href="javascript:;" onclick="resetPassword(\'' + data + '\')"><i class="sie-btn-reset"></i><span><spring:message code="label.user.reset.password"/></span></a>' +
                                    '<a href="javascript:;" onclick="disabled(\'' + data + '\')"><i class="sie-btn-stop"></i><span><spring:message code="label.user.disabled"/></span></a>' +
                                    '<a href="javascript:;" onclick="enabled(\'' + data + '\')"><i class="sie-btn-start"></i><span><spring:message code="label.user.enabled"/></span></a>' +
                                    '<a href="javascript:;" onclick="deleteUser(\'' + data + '\')"><i class="sie-btn-delete"></i><span><spring:message code="label.user.delete"/></span></a></div>';
                        }

                        return divString;
                    }
                }
            ]
        };
        var uri = window.location.href;
        console.log(uri);
        if (!(uri.indexOf("lang=en")>0)) {
            options["language"]={url: '/lang/datatable.chs.json'};
        }
        var table = $('#user_list').DataTable(options);
        $('#inputSuccess2').bind('keyup', function () {
            table.search(this.value).draw();
        });
    });

    function resetPassword(userId) {
        swal({
            title: '<spring:message code="label.user.reset.password"/>',
            text: '<spring:message code="label.tenant.sure.reset.password"/>',
            type: "warning",
            showCancelButton: "true",
            showConfirmButton: "true",
            confirmButtonText: '<spring:message code="label.admin.confirm"/>',
            cancelButtonText: '<spring:message code="label.cancel"/>',
            animation: "slide-from-top"
        }, function () {
            $.ajax({
                type: "post",
                url: "/admin/api/v1/user/initPassword",
                data: JSON.stringify({userId: userId}),
                contentType: "application/json;charset=utf-8",
                success: function (data) {
                    if(data.result == "SUCCESS"){
                        swal("密码重置成功",data.data,"success");
//                        swal("密码重置成功","密码重置成功","success");
                    }else {
                        swal("Error",'<spring:message code="label.tenant.reset.password.fail"/>',"error");
                    }
                    var table = $('#user_list').DataTable();
                    table.ajax.reload();
                },
                error: function (e) {
                    console.log(e);
                    swal("Error!", "", "error");
                }
            });
        });
    }

    function deleteUser(userId) {
        console.log("del:" + userId);
        swal({
            title: '<spring:message code="label.delete.account"/>',
            text: '<spring:message code="label.sure.delete.account"/>',
            type: "warning",
            showCancelButton: "true",
            showConfirmButton: "true",
            confirmButtonText: '<spring:message code="label.admin.confirm"/>',
            cancelButtonText: '<spring:message code="label.cancel"/>',
            animation: "slide-from-top"
        }, function () {
            $.ajax({
                type: "post",
                url: "/admin/api/v1/user/setEnabled",
                data: JSON.stringify({userId: userId,enabled:-1}),
                contentType: "application/json;charset=utf-8",
                success: function (data) {
                    if (data.data !== null && data.data != "" && data.data != undefined) {
                        alert(data.data);
                    }
                    var table = $('#user_list').DataTable();
                    table.ajax.reload();
                },
                error: function (e) {
                    console.log(e);
                    swal("Error!", "", "error");
                }
            });
        });
    }

    function disabled(userId) {
        console.log("dis:" + userId);
        swal({
            title: '<spring:message code="label.tenant.disable.account"/>',
            text: '<spring:message code="label.tenant.sure.disable.account"/>',
            type: "warning",
            showCancelButton: "true",
            showConfirmButton: "true",
            confirmButtonText: '<spring:message code="label.admin.confirm"/>',
            cancelButtonText: '<spring:message code="label.cancel"/>',
            animation: "slide-from-top"
        }, function () {
            $.ajax({
                type: "post",
                url: "/admin/api/v1/user/setEnabled",
                data: JSON.stringify({userId: userId,enabled:0}),
                contentType: "application/json;charset=utf-8",
                success: function (data) {
                    if (data.data !== null && data.data != "" && data.data != undefined) {
                        alert(data.data);
                    }
                    var table = $('#user_list').DataTable();
                    table.ajax.reload();
                },
                error: function (e) {
                    console.log(e);
                    swal("Error!", "", "error");
                }
            });
        });
    }

    function enabled(userId) {
        swal({
            title: '<spring:message code="label.tenant.enable.account"/>',
            text: '<spring:message code="label.tenant.sure.enable.account"/>',
            type: "warning",
            showCancelButton: "true",
            showConfirmButton: "true",
            confirmButtonText: '<spring:message code="label.admin.confirm"/>',
            cancelButtonText: '<spring:message code="label.cancel"/>',
            animation: "slide-from-top"
        }, function () {
            $.ajax({
                type: "post",
                url: "/admin/api/v1/user/setEnabled",
                data: JSON.stringify({userId: userId,enabled:1}),
                contentType: "application/json;charset=utf-8",
                success: function (data) {
                    if (data.data !== null && data.data != "" && data.data != undefined) {
                        alert(data.data);
                    }
                    var table = $('#user_list').DataTable();
                    table.ajax.reload();
                },
                error: function (e) {
                    console.log(e);
                    swal("Error!", "", "error");
                }
            });
        });
    }
</script>
